﻿var InsertCalculatorShip = function (hotelTypeItem, nationalItem, cityItem, hotelItem, tourItem, roomItem,
    dateBeginItem, dateEndItem, quantityAdultItem, quantityChildItem, priceAdultItem, priceChildItem,
    vatItem, totalPriceHotel, countNightItem, seasonTour, tourInfo, priceBabyItem, quantityBabyItem,scheduleShipElement,noteElement,userIdElement) {



    var loadPriceRoom = function () {
        var roomId = $(roomItem).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "GetPrice", "priceId": roomId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultItem).val(data.PriceAdult);
                    $(priceChildItem).val(data.PriceChild);
                    $(priceBabyItem).val(data.PriceBaby);
                }
            }
        });
    };
    $(roomItem).change(loadPriceRoom);
    //ham them sua xoa
    var fncCalculatorShip = function () {
      
        var typeId = 10;
        var tourId = parseInt(tourItem);
        //room =PriceId
        var roomId = $(roomItem).val();
        var dateBegin = $(dateBeginItem).val();
        var dateEnd = $(dateEndItem).val();
        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var quantityBaby = $(quantityBabyItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var priceBaby = $(priceBabyItem).val();
        var vat = $(vatItem).val();
        var note = $(noteElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var concern = $(hotelItem).val();
        var userId = parseInt(userIdElement);
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: {
                "function": "InsertCalculatorShip", "TypeId": typeId, "TourId": tourId, "RoomId": roomId,
                "DateBegin": dateBegin, "DateEnd": dateEnd, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild,
                "PriceAdult": priceAdult, "PriceChild": priceChild, "VAT": vat, "CalculatorId": calculatorId,
                "ClassName": className, "PriceBaby": priceBaby, "QuantityBaby": quantityBaby, 'Note': note,
                'Concern': concern, 'UserId': userId
            },
            dataType: "json",
            success: function (data) {
                $(".ListShip").remove();
                $(hotelTypeItem).val('-1');
                $(nationalItem).val('-1');
                $(cityItem).val('-1');
                $(roomItem).val('-1');
                $(dateBeginItem).val('');
                $(dateEndItem).val('');
                $(quantityAdultItem).val('');
                $(quantityChildItem).val('');
                $(quantityBabyItem).val('');
                $(priceAdultItem).val('');
                $(priceChildItem).val('');
                $(priceBabyItem).val('');
                $(vatItem).val('');
                $(countNightItem).text('');
                $(totalPriceHotel).text('');
                $(noteElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-Ship')
                            .before($("<tr class='ListShip' id='ticket-Ship-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.ScheduleName + '</td>' +
                                '<td >' + item.Hotel + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.DateEnd + '</td>' +
                                '<td>' + item.CountNight + '</td>' +
                                '<td >' + item.RoomType + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td>' + item.Total + '</td>' +
                                '<td >' + item.VAT + '</td>' +
                                '<td> </td>' +
                                '<td>' +
                                '<input class="btnUpdateShip" onclick="openPopup(\'ticket-ship\')" type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteShip"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalTicketShipPrices').html(data.TotalTicketHotelPrices);
                    $('#TotalTicketShipPricesAdults').html(data.TotalTicketHotelPricesAdults);
                    $('#TotalTicketShipPricesChild').html(data.TotalTicketHotelPricesChild);
                    $('#TotalTicketShipPricesBaby').html(data.TotalTicketHotelPricesBaby);
                    $('#TotalVatShip').html(data.TotalVatShip);
                    $('#btnInsertShip').attr("calculatorid", "-1").val("Thêm");
                    TotalPrice();
                    fncTotalCalculator();
                    hidePopup('ticket-ship');
                }
            }
        });
        
    };
    $('#btnInsertShip').on("click", fncCalculatorShip);
    $('#btnDeleteShip').on("click", fncCalculatorShip);
    $('table').on("click", 'tr >td >input.btnDeleteShip',fncCalculatorShip);
    $('table').on("click", 'tr >td >input.btnUpdateShip', function () {
        var calculatorId = $(this).attr("calculatorid");

        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "UpdateCalculatorShip", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#ticket-Ship-' + calculatorId).remove();
                $(hotelTypeItem).val(data.HotelTypeId);
                $(nationalItem).combobox('setValue', data.NationalName);
                $(nationalItem).val(data.Nationalid);
                $(scheduleShipElement).combobox('setValue', data.ScheduleName);
                $(scheduleShipElement).val(data.ScheduleId);
                loadCityNationalid(data.CityId, data.HotelId, data.PriceId, data.CityName,data.HotelName);
                $(dateBeginItem).val(data.BeginDate);
                $(dateEndItem).val(data.EndDate);
                $(quantityAdultItem).val(data.QuantityAdults);
                $(quantityChildItem).val(data.QuantityChild);
                $(quantityBabyItem).val(data.QuantityBaby);
                $(priceAdultItem).val(data.PriceAdults);
                $(priceChildItem).val(data.PriceChild);
                $(priceBabyItem).val(data.PriceBaby);
                $(vatItem).val(data.Vat);
                $(noteElement).val(data.Note);
                $(countNightItem).text(fncCountNightauto(data.BeginDate, data.EndDate));
                $(totalPriceHotel).text(data.TotalPriceHotel);
                $('#btnInsertShip').attr("calculatorid", calculatorId).val("Lưu");
                
            }
        });
    });
    //load city
    var loadCityNationalid = function (cityItemId, hotelId, priceId, cityName,hotelName) {
        var nationalId = $(nationalItem).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "ListCityByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(cityItem).html('<option value="-1">--- Chọn tỉnh/thành phố ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(cityItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });

                    $(cityItem).combobox('setValue', cityName);
                    $(cityItem).val(cityItemId);
                    fncHotel(hotelId, priceId,  hotelName);
                }
            }
        });
    };
    $(nationalItem).combobox({
        select: function (event, ui) {
            loadCityNationalid();
        }
    });
    //load Hotel
    var fncHotel = function (hotelId, priceId,hotelName) {
        var hotelType = $(hotelTypeItem).val();
        var city = $(cityItem).val();
        var scheduleShip = $(scheduleShipElement).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "ListShipByHotelType", "HotelTypeId": hotelType, "CityId": city, "Item_Type": 10, 'ScheduleShip': scheduleShip },
            dataType: "json",
            success: function (data) {
                $(hotelItem).html('<option value="-1">--- Chọn khách sạn---</option>');
                if(data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(hotelItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });

                    $(hotelItem).combobox('setValue', hotelName);
                    $(hotelItem).val(hotelId);
                    loadRoomTypeByHotel(priceId);
                }
            }
        });
    };
    $(hotelTypeItem).change(fncHotel);
    $(cityItem).combobox({
        select: function (event, ui) {
            fncHotel();
        }
    });
    //Load loai
    var loadRoomTypeByHotel = function (priceId) {
        var hotelId = $(hotelItem).val();
        var season = seasonTour;
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "ListRoomTypeByShip", "HotelId": hotelId, "Season": season },
            dataType: "json",
            success: function (data) {
                $(roomItem).html('<option value="-1">--- Chọn loại phòng---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(roomItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                    $(roomItem).val(priceId);
                }
            }
        });
    };
    $(hotelItem).combobox({
        select: function (event, ui) {
            loadRoomTypeByHotel();
        }
    });
    var diffDays = 0;
    var fncCountNight = function () {
        if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
            var dateBegin = $(dateBeginItem).val().split("/");
            var dateEnd = $(dateEndItem).val().split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
            if (diffDays == 0) {
                diffDays = 1;
            }
            $(countNightItem).text(diffDays);
        }
    };
    $(dateBeginItem).change(fncCountNight);
    $(dateEndItem).change(fncCountNight);
    var fncCountNightauto = function (dateBeginauto, dateEndauto) {
        if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
            var dateBegin = dateBeginauto.split("/");
            var dateEnd = dateEndauto.split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
            if (diffDays == 0) {
                diffDays = 1;
            }
            $(countNightItem).text(diffDays);
        }
    };
    var fncTotalPriceRoom = function () {

        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var quantityBaby = $(quantityBabyItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var priceBaby = $(priceBabyItem).val();
        var vat = $(vatItem).val();
        if (vat.toString() == NaN) vat = 0;
        var total = (quantityAdult * priceAdult +quantityChild * priceChild + quantityBaby * priceBaby)  * diffDays;
        $(totalPriceHotel).text(total);
        $(vatItem).val(total*0.1);
    };
    $(quantityAdultItem).keyup(fncTotalPriceRoom);
    $(quantityChildItem).keyup(fncTotalPriceRoom);
    $(quantityBabyItem).keyup(fncTotalPriceRoom);
    $(priceAdultItem).keyup(fncTotalPriceRoom);
    $(priceBabyItem).keyup(fncTotalPriceRoom);
    $(priceChildItem).keyup(fncTotalPriceRoom);
 
    $(dateBeginItem).change(fncTotalPriceRoom);
    $(dateEndItem).change(fncTotalPriceRoom);
};